package org.ranch.web.propertyeditors;

import org.apache.commons.lang.StringUtils;
import org.ranch.api.service.ConceptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * PropertyEditor that supports concept data binding in the controllers
 * 
 * @author ctumwebaze
 * 
 */
@Component("conceptPropertyEditor")
public class ConceptPropertyEditor extends BasePropertyEditor {

    @Autowired
    private ConceptService conceptService;

    /*
     * (non-Javadoc)
     * 
     * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
	if (StringUtils.isNotBlank(text) && StringUtils.isNotEmpty(text)) {
	    if (StringUtils.equalsIgnoreCase("none", text)) {
		super.setValue(null);
	    } else {
		super.setValue(conceptService.getConceptById(text));
	    }
	} else {
	    super.setAsText(text);
	}
    }

}
